package com.echo.blog.core.utils;


import com.echo.blog.business.sys.model.SysThirdUser;
import com.echo.blog.business.sys.model.SysUser;
import com.echo.blog.core.properties.Oauth2ClientProperties;
import com.echo.model.constant.ConstantUtils;
import com.echo.model.utils.ValueUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @version V1.0
 * @Project：ffba-portal
 * @Title：RestClient.java
 * @Description：
 * @Package com.ws.ztyt.portal.core.utils
 * @date：2019年07月03日
 * @Copyright: 武汉中天云通数据科技有限责任公司  All rights reserved.
 */
@Slf4j
@Component
public class RestClient {

    @Resource
    private RestTemplate webClient;

    @Resource
    private Oauth2ClientProperties clientProperties;


    private static final HttpHeaders headers = new HttpHeaders();


    public void init() {
        //后台提供正常的 资源认证标识
        String userMsg = clientProperties.getClientId() + ":" + clientProperties.getClientSecret();
        String base64UserMsg = "Basic " + Base64.getEncoder().encodeToString(userMsg.getBytes());
        headers.clear();
        headers.add("Authorization", base64UserMsg);
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //headers.add("api-key", bookingApiKey);添加 额外参数
    }


    /**
     * @Author chentailai
     * @Description 从认证服务器 通过密码模式 获取 access_token
     * @Param [sysUser, responseType]
     * @Return F
     * @Date 2020/3/30 22:30
     */
    public <F, T> F getOauthTokenByPasswordMode(SysUser sysUser, Class<F> responseType) {
        try {
            init();
            MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
            multiValueMap.add(ConstantUtils.Public.USER_NAME, sysUser.getUsername());
            multiValueMap.add(ConstantUtils.Public.PASS_WORD, sysUser.getPassword());
            multiValueMap.add(ConstantUtils.Public.GRANT_TYPE, ConstantUtils.Public.PASS_WORD);
            multiValueMap.add(ConstantUtils.Public.SCOPE, ConstantUtils.Public.ALL);
            HttpEntity<MultiValueMap<String, String>> formEntity = new HttpEntity<>(multiValueMap, headers);
            ResponseEntity<F> responseEntity = this.webClient.postForEntity(clientProperties.getAccessTokenUri(), formEntity, responseType);
            return responseEntity.getBody();
        } catch (Exception e) {
            log.error("请求授权认证时出现异常", e);
        }
        return null;
    }


    /**
     * 将客户端
     *
     * @param responseType
     * @param <F>
     * @param <T>
     * @return
     */
    public <F, T> F getApi(SysThirdUser sysUser, Class<F> responseType) {
        try {
            init();
            Map<String, String> param = new HashMap<>();
            param.put("username", sysUser.getLogin());
            param.put("password", ValueUtils.parseString(sysUser.getThirdId()));
            param.put("scope", "all");
            HttpEntity<Map> formEntity = new HttpEntity<>(null, headers);
            ResponseEntity<F> responseEntity = this.webClient.exchange(clientProperties.getAccessTokenUri(), HttpMethod.GET, formEntity, responseType, param);
            return responseEntity.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
